import os

import mne
EEG_path = 'C:\\Users\\webrain\\Desktop\\feng\\testdata\\zzj_1108\\fb_zzj.vhdr'
EDF_path = 'C:\\Users\\webrain\\Desktop\\feng\\testdata\\TEST.EDF'

# def load_raw(EEG_path):
#     file_ext = os.path.splitext(EEG_path)[1]  # 扩展名
#     if file_ext == ".vhdr":
#         raw = mne.io.read_raw_brainvision(EEG_path, verbose=False, preload=False)
#     elif file_ext == ".EDF":
#         raw = mne.io.read_raw_edf(EEG_path, verbose=False, preload=False)
#     return raw
#
# raw = load_raw(EEG_path)
raw = mne.io.read_raw_brainvision(EEG_path, preload=False)
# 获取 BP 设备的导联信息
montage = raw.get_montage()
print(montage)
# 转换为与 EEG 结构体相同的形式
ch_names = [ch_name.replace('.', '') for ch_name in montage.ch_names]
pos = {ch_name: montage.pos[i] for i, ch_name in enumerate(montage.ch_names)}
ch_pos = {ch_name: pos[ch_name] for ch_name in ch_names}
ch_pos_scaled = {ch_name: list(map(float, pos)) for ch_name, pos in ch_pos.items()}
ch_types = ['eeg'] * len(ch_names)
chanlocs = mne.create_info(ch_names=ch_names, sfreq=eeg.srate, ch_types=ch_types).ch_names
for ch_name, pos in ch_pos_scaled.items():
    idx = chanlocs.index(ch_name)
    chanlocs[idx].x, chanlocs[idx].y, chanlocs[idx].z = pos
#----------------------------------------------
# events_from_vmrk = mne.events_from_annotations(raw)
# events, event_id = events_from_vmrk[:2]
# print("-------------")
# print(events)
# print("-------------")
# print(event_id)
# epochs = mne.Epochs(raw, events=events, event_id=event_id)
# print("-------------")
# print(epochs)
#-------------------------------------------------
# 获取导联信息
# channels = raw.info['chs']
# print(channels)
# for ch in channels:
#     print(f"Channel name: {ch['ch_name']}, Location: {ch['loc']}")
# data = mne.io.read_raw_brainvision(
#     self.vhdr_path, verbose=False).get_data()

print("=================================")
# 读取EDF文件
raw = mne.io.read_raw_edf(EDF_path, preload=False)
data = raw.get_data()
# 打印基本信息
#
# print(raw.info)
# print("=====================")
# # 获取导联信息
# # channels = raw.info['chs']
# # print(channels)
# #
# # data_slid = data[:,1]
# # print(data_slid)
#
# channels = raw.info['chs']
# print(channels)
# for ch in channels:
#     print(f"Channel name: {ch['ch_name']}, Location: {ch['loc']}")
# print("------")
# events, event_id = mne.events_from_annotations(raw)
# print(events)
# print(event_id)
